package com.example.wan1.view.login;

import com.example.wan1.data.BaseObserver;
import com.example.wan1.data.RetrofitUtil;
import com.example.wan1.data.RxSchedulers;
import com.example.wan1.model.PersonBean;
import com.example.wan1.utils.AppUtils;

import javax.inject.Inject;

/***
 * 创建时间：2020/2/10 18:41
 * 创建人：10850
 * 功能描述：
 */
public class LoginPresenter implements LoginContract.Presenter {

    @Inject
    LoginContract.View mView;

    @Inject
    public LoginPresenter() {
    }

    @Override
    public void login(String name, String pwd) {
        RetrofitUtil.getInstance().postLogin(name, pwd)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<PersonBean>() {
                    @Override
                    public void onSuccess(PersonBean result) {
                        mView.loginSuccess(result);
                        RetrofitUtil.tryLogin = false;
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.loginFailure(e.getMessage());
                        RetrofitUtil.tryLogin = false;
                    }
                });
    }

    @Override
    public void register(String name, String pwd, String repwd) {
        RetrofitUtil.getInstance().postRegister(name, pwd, repwd)
                .compose(RxSchedulers.observableIO2Main())
                .subscribe(new BaseObserver<PersonBean>() {
                    @Override
                    public void onSuccess(PersonBean result) {
                        //这里可以直接登录
                        //AppUtils.setIsLogin(true);
                        RetrofitUtil.refresh();//登录成功后需要先加上cookie
                        login(name, pwd);//改了
                    }

                    @Override
                    public void onFailure(Throwable e, String errorMsg) {
                        mView.loginFailure(e.getMessage());
                    }
                });
    }
}